﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using Newtonsoft.Json.Linq;

namespace Chat_WebServer.Controllers
{
    public class SearchController : ApiController
    {
        public object GET(string name, float lat, float lng, int radius)
        {
            string location = "&location=" + lat.ToString() + "," + lng.ToString();
            string rad = "&radius=" + radius;
            string requestURL = "https://maps.googleapis.com/maps/api/place/textsearch/json?sensor=true&key=AIzaSyD6wgGyWqi2GRqU0csQ4kXf5nHHYOcxRps&query=" + name + location + rad;
            HttpWebRequest request = HttpWebRequest.Create(requestURL) as HttpWebRequest;
            request.Method = "GET";
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream());
                        String result = reader.ReadToEnd();
                        return JObject.Parse(result);
                    }
                }
            }
            catch
            {
                
            }
            return null;
        }

        public object GET(float lat, float lng, string type, int radius)
        {
            string location = "&location=" + lat.ToString() + "," + lng.ToString();
            string rad = "&radius=" + radius;
            string tp = "&types=" + type;
            string requestURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?sensor=true&key=AIzaSyD6wgGyWqi2GRqU0csQ4kXf5nHHYOcxRps" + location + rad;
            if (type != "") requestURL += tp;
            HttpWebRequest request = HttpWebRequest.Create(requestURL) as HttpWebRequest;
            request.Method = "GET";
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream());
                        String result = reader.ReadToEnd();
                        return JObject.Parse(result);
                    }
                }
            }
            catch
            {

            }
            return null;
        }
    }
}
